<?php

namespace Products;

use Bank\BankUtils;

class HomeLoan extends Loan {
	const MIN_PERIOD_HOME = 60;
	const MAX_PERIOD_HOME = 300;
	const MIN_AMOUNT_HOME = 0.00;
	const MAX_AMOUNT_HOME = 100000.00;
	const HOME_LOAN_INTERESTRATE = 6;
	private $period;
	private $amount;
	public function __construct($period, $amount) {
		$this->setPeriod ( $period );
		$this->setAmount ( $amount );
	}
	public function getPeriod() {
		return $this->period;
	}
	public function setPeriod($period) {
		if ($period == null) {
			throw new BaseException ( "Please enter home loan period!", BaseException::CODE_2001 );
		}
		if (is_int ( $period ) = ! true) {
			throw new BaseException ( "Please enter number for home lone period!", BaseException::CODE_2002 );
		}
		if ($period < self::MIN_PERIOD_HOME || $period > self::MAX_PERIOD_HOME) {
			throw new BaseException ( "Please enter period between 60 months and 300 months !", BaseException::CODE_2003 );
		}
		
		$this->period = $period;
	}
	public function getAmount() {
		return $this->amount;
	}
	public function setAmount($amount) {
		if ($amount == null) {
			throw new BaseException ( "Please enter home loan amount!", BaseException::CODE_2004 );
		}
		if (is_double ( $amount ) != true) {
			throw new BaseException ( "Please enter XXX.XX amount!", BaseException::CODE_2005 );
		}
		if ($amount < self::MIN_AMOUNT_HOME || $amount > self::MAX_AMOUNT_HOME) {
			throw new BaseException ( "Please enter home loan amount between 0 and 100000!", BaseException::CODE_2006 );
		}
		
		$this->amount = $amount;
	}
	public function reduceAmount() {
		echo '<p>Reduce Amount</p>';
		echo '<p>Home Loan - amount :' . $this->getAmount () . '</p>';
		echo '<p>Home Loan - monthly payment :' . BankUtils::calculateCreditMonthlyPayment ( $this->getAmount (), self::HOME_LOAN_INTERESTRATE, $this->getPeriod () ) . '</p>';
		$this->setAmount ( $this->getAmount () - BankUtils::calculateCreditMonthlyPayment ( $this->getAmount (), self::HOME_LOAN_INTERESTRATE, $this->getPeriod () ) );
		echo '<p>Home Loan - reduce amount :' . $this->getAmount () . '</p>';
	}
	public function monthlyPayment() {
		BankUtils::calculateCreditMonthlyPayment ( $this->getAmount (), self::HOME_LOAN_INTERESTRATE, $this->getPeriod () );
	}
	public function showInfo() {
		echo '<p>Home Loan</p>';
		echo '<p>Home Loan - period : ' . $this->getPeriod () . '</p>';
		echo '<p>Home Loan - amount : ' . $this->getAmount () . '</p>';
		echo '<p>Home Loan - interestRate : ' . self::HOME_LOAN_INTERESTRATE . ' % </p>';
		echo '<p>Home Loan - monthly payment : ' . BankUtils::calculateCreditMonthlyPayment ( $this->getAmount (), self::HOME_LOAN_INTERESTRATE, $this->getPeriod () ) . '</p>';
	}
}
?>